<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
            <el-form-item label="公司名称" prop="cname">
                <el-input v-model="queryParams.cname" placeholder="请输入公司名称" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="checkstatus">
                <el-select v-model="queryParams.checkstatus" placeholder="请选择状态" clearable size="small">
                    <el-option label="待审核" value="0" />
                    <el-option label="审核通过,待复审" value="1" />
                    <el-option label="审核不通过" value="2" />
                    <el-option label="复审通过" value="3" />
                    <el-option label="复审不通过" value="4" />
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['biz:uplog:export']">导出</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="uplogList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" prop="id" />
            <el-table-column label="公司名称" align="center" prop="cname" />
            <el-table-column label="付款时间" align="center" prop="paytime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.paytime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="付款方式" align="center" prop="paytype">
                <template slot-scope="scope">
                    <span>{{ scope.row.paytype | paytypes }}</span>
                </template>
            </el-table-column>
            <el-table-column label="付款币种类型" align="center" prop="payrbtype">
                <template slot-scope="scope">
                    <span>{{ scope.row.payrbtype | payrbtypes }}</span>
                </template>
            </el-table-column>
            <el-table-column label="付款金额" align="center" prop="payfee" />
            <el-table-column label="银行名称" align="center" prop="bankname" />
            <el-table-column label="付款账号" align="center" prop="payaccount" width="180px;" />
            <!-- <el-table-column label="充值状态" align="center" prop="upstatus" /> -->
            <el-table-column label="审核状态" align="center" prop="checkstatus" :formatter="formatterfuc" />
            <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)" v-hasPermi="['biz:uplog:edit']">{{
                        scope.row.checkstatus == '0' ? '未审核' : scope.row.checkstatus == '2' ? '审核未通过' : scope.row.checkstatus == '1' || scope.row.checkstatus > '2' ? '审核通过' : ''
                    }}</el-button>

                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 2)" v-hasPermi="['biz:uplog:edit']" :disabled="scope.row.checkstatus == '0' || scope.row.checkstatus == '2'">{{
                        scope.row.checkstatus == 0 || scope.row.checkstatus == 2 ? '等待复审' : scope.row.checkstatus == 3 ? '复审通过' : scope.row.checkstatus == 4 ? '复审未通过' : '等待复审'
                    }}</el-button>

                    <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:uplog:remove']"
          >删除</el-button> -->
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改充值申请对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="cancel">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <div class="flex">
                    <el-form-item label="付款时间" prop="paytime" :disabled="disabled">
                        <el-date-picker clearable size="small" style="width: 200px" v-model="form.paytime" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择付款时间" :disabled="disabled"> </el-date-picker>
                    </el-form-item>
                    <el-form-item label="付款方式">
                        <el-select v-model="form.paytype" placeholder="请选择付款方式" :disabled="disabled">
                            <el-option v-for="(item, index) in paytypeOptions" :key="index" :label="item" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="付款币种类型">
                        <el-select v-model="form.payrbtype" placeholder="请选择付款币种类型" :disabled="disabled">
                            <el-option v-for="(item, index) in paycategoryOptions" :key="index" :label="item" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="付款金额" prop="payfee">
                        <el-input v-model="form.payfee" placeholder="请输入付款金额" :disabled="disabled" />
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="银行名称" prop="bankname">
                        <el-input v-model="form.bankname" placeholder="请输入银行名称" :disabled="disabled" />
                    </el-form-item>
                    <el-form-item label="付款账号" prop="payaccount">
                        <el-input v-model="form.payaccount" placeholder="请输入付款账号" :disabled="disabled" />
                    </el-form-item>
                </div>
                <el-form-item label="付款凭证">
                    <el-upload ref="legalIdcardFile" :headers="headers" :on-success="idcardUploadSuccess" :file-list="legalIdcardFile" :action="uploadImgUrl" :before-upload="fieldBeforeUpload" list-type="picture-card" :disabled="disabled">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer" v-show="ishows">
                <el-button type="primary" @click="submitForms(1)">审核通过</el-button>
                <el-button @click="submitForms(2)">审核不通过</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listUplog, getUplog, delUplog, addUplog, updateUplog, exportUplog } from '@/api/biz/uplog';
import { getToken } from '@/utils/auth';

export default {
    name: 'Uplog',
    data() {
        return {
            ishows: false,
            disabled: true,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            paytypeOptions: [],
            paycategoryOptions: [],
            // 总条数
            total: 0,
            // 充值申请表格数据
            uplogList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                uid: undefined,
                cname: undefined,
                paytime: undefined,
                paytype: undefined,
                payrbtype: undefined,
                payfee: undefined,
                bankname: undefined,
                payaccount: undefined,
                payfile: undefined,
                checkstatus: undefined,
                createdTime: undefined,
                status: undefined
            },
            ishow: true,
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            paytypeOptions: [],
            paycategoryOptions: [],
            // 上传
            legalIdcardFile: [],
            uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
            headers: {
                Authorization: 'Bearer ' + getToken()
            },
            currentrow: undefined,
            fileshost: process.env.VUE_APP_BASE_API
        };
    },
    filters: {
        // 付款方式
        paytypes(val) {
            switch (val) {
                case 1:
                    return '转账';
                    break;
                case 2:
                    return '汇款';
                    break;
            }
        },
        // 付款币种类型
        payrbtypes(val) {
            switch (val) {
                case 1:
                    return '人民币';
                    break;
                case 2:
                    return '日元';
                    break;
                case 3:
                    return '美元';
                    break;
                case 4:
                    return '欧元';
                    break;
            }
        }
    },
    created() {
        this.getDicts('pay_type').then(response => {
            // console.log('*************');
            // console.log(response.data);
            // console.log('*************');

            response.data.forEach((item, index) => {
                this.paytypeOptions[item.dictValue] = item.dictLabel;
            });
        });
        this.getDicts('pay_category').then(response => {
            response.data.forEach((item, index) => {
                this.paycategoryOptions[item.dictValue] = item.dictLabel;
            });
        });

        this.getList();
    },
    methods: {
        formatterfuc: function(row, clo) {
            var status = '';

            switch (row.checkstatus) {
                case 0:
                    status = '待审核';
                    break;
                case 1:
                    status = '审核通过，等待复审';
                    break;
                case 2:
                    status = '审核不通过';
                    break;
                case 3:
                    status = '复审通过';
                    break;
                case 4:
                    status = '复审未通过';
                    break;
                default:
                    status = '未知';
                    break;
            }

            return status;
        },
        idcardUploadSuccess(response) {
            // console.log(response)
            // if (response.code == 200) {
            //     this.legalIdcardFile.push(response.fileName);
            //     // this.form.payfile =response.fileName;
            //     console.log(this.legalIdcardFile)
            // }
        },
        fieldBeforeUpload(file) {
            let isRightSize = file.size / 1024 / 1024 < 2;
            if (!isRightSize) {
                this.$message.error('文件大小超过 2MB');
            }
            return isRightSize;
        },
        idcardRemove(obj) {
            var tmpimg = obj.url.substr(8);
            this.legalIdcardFile.forEach((item, index) => {
                if (item == tmpimg) {
                    this.legalIdcardFile.splice(index, 1);
                }
            });
        },
        fileChange() {},
        operateUploadSuccess(response) {
            if (response.code == 200) {
                this.legalOperateFile.push(response.fileName);
            }
        },
        /** 查询充值申请列表 */
        getList() {
            this.loading = true;
            listUplog(this.queryParams, 1).then(response => {
                this.uplogList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            console.log(123132131312);
            this.legalIdcardFile = [];
            this.$refs['legalIdcardFile'].clearFiles();
            this.open = false;
            this.reset();
            this.getList();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                uid: undefined,
                cname: undefined,
                paytime: undefined,
                paytype: undefined,
                payrbtype: undefined,
                payfee: undefined,
                bankname: undefined,
                payaccount: undefined,
                legalIdcardFile: [],
                payfile: undefined,
                checkstatus: '0',
                createdTime: undefined,
                status: '0'
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
            this.queryParams.cname = '';
            this.queryParams.checkstatus = '';
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加充值申请';
        },
        /** 修改按钮操作 */
        handleUpdate(row, mark) {

            this.legalIdcardFile = [];
            this.ishows = false;
            this.reset();
            const id = row.id || this.ids;
            getUplog(id).then(response => {
                this.form = response.data;
                if (this.form.payfile != undefined) {
                    let filesList = this.form.payfile.split(',');
                    filesList.forEach((item, index) => {
                        this.legalIdcardFile.push({
                            name: 'cdsc',
                            url: this.fileshost + item
                        });
                    });
                }

                if (mark == 1 && this.form.checkstatus == 0) {
                    this.ishows = true;
                }

                if (mark == 2 && this.form.checkstatus == 1) {
                    this.ishows = true;
                }

                this.currentrow = this.form;
                this.open = true;
            });
        },
        /** 提交按钮 */
        submitForm: function() {
            this.form.payfile = this.legalIdcardFile.join(',');
            this.$refs['form'].validate(valid => {
                if (valid) {
                    let message = '修改成功';
                    if (this.form.id != undefined) {
                        this.form.upstatus = 1;

                        updateUplog(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(message);
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        //审核通过
        submitForms: function(checkstatus) {
            //未审核
            if (this.currentrow.checkstatus == 0) {
                if (checkstatus == 1) {
                    this.currentrow.checkstatus = 1;
                } else {
                    this.currentrow.checkstatus = 2;
                }
            } else if (this.currentrow.checkstatus == 1) {
                if (checkstatus == 1) {
                    this.currentrow.checkstatus = 3;
                } else {
                    this.currentrow.checkstatus = 4;
                }
            } else {
                return;
            }

            //return;

            updateUplog(this.currentrow).then(response => {
                if (response.code === 200) {
                    this.msgSuccess('ok,操作成功');
                    this.open = false;
                    this.getList();
                    this.$refs['legalIdcardFile'].clearFiles();
                } else {
                    this.msgError(response.msg);
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除充值申请编号为"' + ids + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function() {
                    return delUplog(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess('删除成功');
                })
                .catch(function() {});
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出充值申请数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function() {
                    return exportUplog(queryParams);
                })
                .then(response => {
                    //this.download(response.msg);
                    this.msgSuccess(response.msg);
                })
                .catch(function() {});
        }
    }
};
</script>
